#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <librtmp/rtmp.h>
#include <librtmp/log.h>
#include "base/Thread.h"
#include "MemAudio.h"
#define RECEIVE_FILE "receive.m4a"
//ACC音频ADTS
typedef struct tagAACDecoderSpecific
{
	unsigned char nAudioFortmatType;	//音频编码类型（0：Liner PCM platform endian，1：PCM，2：mp3，4：Nellymoser 16-kHz mono,5:Nellymoser 8-kHz mono,6:Nellymoser,7:G.711 A-law logarithmic PCM,8:G.711 mu-law logarithmic PCM,9:reserved,10: AAC,14:MP3 8-Khz,15:Device-specific sound）
	unsigned char nAudioSampleType;	//音频采样率(0:5.5kHz,1:11KHz,2:22 kHz,3:44 kHz)
	unsigned char nAudioSizeType;	//音频采样精度(0:8bits,1:16bits)
	unsigned char nAudioStereo;//是否立体声(0:sndMono,1:sndStereo)
	unsigned char nAccPacketType;
}AACDecoderSpecific;

typedef struct tagAudioSpecificConfig
{
	unsigned char nAudioObjectType;
	unsigned char nSampleFrequencyIndex;
	unsigned char nChannels;
	unsigned char nFrameLengthFlag;
	unsigned char nDependOnCoreCoder;
	unsigned char nExtensionFlag;
}AudioSpecificConfig;
typedef unsigned char byte;

class RtmpAudio:public CEventThread
{
public:
	RtmpAudio(MemAudio* pMem){
		pMemAudio = pMem;
		fpAAC = fopen(RECEIVE_FILE, "wb");
		if(!fpAAC){
			RTMP_LogPrintf("Open File Error.\n");
		}
		rtmp = NULL;
	}
	virtual ~RtmpAudio(){}

public:
	virtual void OnThreadTick();
	int CreateADTS(AudioSpecificConfig ascAudioSpecificConfig, unsigned short nAudioFrameLength, char *pADTS);
private:
	int Run();
	int initRtmp();
	void destoryRtmp();
private:
	FILE *fpAAC;
	RTMP *rtmp;
	MemAudio* pMemAudio;
};
